﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data.OleDb;


namespace QLTV.HeThongXuLy
{
    class ConnectDB
    {
        //SQl
      //  protected static String _connectionString = "Data Source=it-ptanh; Initial Catalog=QLTV; Integrated Security=TRUE";
        //static SqlConnection connection = new SqlConnection(_connectionString);       

        protected static String _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=data/QLTV.mdb";        
        static  OleDbConnection connection = new OleDbConnection(_connectionString);
       
        public static void OpenConnection()
        {
            try
            {
                //connection = new SqlConnection(_connectionString);
                connection.Open();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message +" : Lỗi kết nối! kiểm tra lại máy chủ. " , "Lỗi kết nối..");
            }
        }

        //public static void CloseConnection()
        public static void CloseConnection()
        {
            if (connection != null)
            {
                connection.Close();
            }
        }
        public static DataTable ExecQuery(string sql)
        {
            ConnectDB.OpenConnection();
            DataTable dt = new DataTable();
            //SqlCommand command = connection.CreateCommand();
            OleDbCommand command = connection.CreateCommand();
            //SqlCommand command = new SqlCommand(sql, connection);
            command.CommandText = sql;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            //SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            CloseConnection();
            return dt;
        }
        public static void ExecNonQuery(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            command.ExecuteNonQuery();
            CloseConnection();
        }
    }
}
